package com.logic.landseaserver.service.bill;

import java.util.Date;

import com.logic.system.service.impl.ConfigurationService;
import org.apache.commons.lang.time.DateUtils;

import com.logic.landseaserver.common.enums.BillTypeEnum;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillDetail;
import com.logic.landseaserver.ws.dto.ContractBillDTO;

/**
 * Created by Junny on 2017/7/3.
 * 房租生成器
 */
public class HourseBillFactory implements BillFactory{

    private ContractBillDTO contract;

    private Integer payMonth;

    private Date startDate;

    private Date endDate;

    private Double billAmount;

    private boolean lastHourseBill;

    private Double dayOfMonth;

    private boolean billChangeFlag;

    public HourseBillFactory(ContractBillDTO contract,Integer payMonth,Integer nowTerm,Double dayOfMonth,boolean billChangeFlag){
        this.contract = contract;
        this.dayOfMonth = dayOfMonth;
        //一个月房租
        this.billAmount = contract.getSalePrice();
        this.payMonth = payMonth;
        payMonth = payMonth-1;
        this.startDate = DateUtil.getInstence().getFirstDayOfMonth(new Date());
        this.endDate = DateUtil.getInstence().getLastDayOfMonth(DateUtils.addMonths(startDate,payMonth));
        //判断是否是最后一次缴纳房租和物业
        this.lastHourseBill = false;
        //判断是否是拆分账单
        this.billChangeFlag = billChangeFlag;
        //应该付几个月的房租
        int howManyMonthPay= contract.getPayM();
        //当期+付X>=租期+1 则认为是最后一期账单
        int leaseM = contract.getLeaseM()+1;
        if(contract.getLeaseD()>0){
            leaseM = leaseM+1;
        }

        if(nowTerm+howManyMonthPay >= leaseM){
            this.endDate = contract.getOutDate();
            this.lastHourseBill = true;
        }
    }

    private Double calculationAmount(){
        if(this.lastHourseBill || billChangeFlag){
            //获取最后一月的总天数
            int dayOfEndMonth = DateUtil.getInstence().getDaysOfMouth(this.endDate);
            // 当月剩余天数
            int ddHave =Integer.parseInt(DateUtil.getInstence().getDd(this.endDate));
            Double daysPrice = 0.0;
            //当月天数=当月剩余天数，则默认为一个月
            if(ddHave==dayOfEndMonth){
                daysPrice= LandSeaUtil.get2Double(this.billAmount);
            }else{
                daysPrice= LandSeaUtil.get2Double(this.billAmount / this.dayOfMonth * ddHave);
            }
            int diffMonth = DateUtil.getInstence().getApartMonth(this.startDate,this.endDate);
            this.billAmount = LandSeaUtil.getIntDouble(daysPrice+(this.billAmount*diffMonth));
        }else{
            this.billAmount = LandSeaUtil.getIntDouble(this.billAmount*this.payMonth);
        }
        return this.billAmount;
    }

    @Override
    public BillDetail createBill() {
        BillDetail bill = new BillDetail();
        bill.setType(BillTypeEnum.CTBI8.getCode());
        bill.setDateBegin(this.startDate);
        bill.setDateEnd(this.endDate);
        bill.setMoney(this.calculationAmount());
        return bill;
    }
}
